package com.qyl.deguru.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.qyl.deguru.common.PageConstant;
import com.qyl.deguru.dto.User;

public class CustomerLoginInterceptor implements HandlerInterceptor {

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		//判断用户是否登录
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		//用户不为空，放行
		if(user!=null){
			return true;
		}else{
			//用户为null，则跳转到登录页面
			response.sendRedirect(request.getContextPath()+"/customer/toLogin");
			return false;
		}
		
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

}
